package com.ddwl.goods.service;

import org.springframework.util.CollectionUtils;
import com.ddwl.common.feign.api.OrderApi;
import com.ddwl.common.util.MySqlUtil;
import com.ddwl.common.util.ObjectUtil;
import com.ddwl.common.util.UUIDGenerator;
import com.ddwl.goods.dao.ItemDao;
import com.ddwl.goods.mapper.CollectMapper;
import com.ddwl.goods.model.Collect;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.goods.CollectBo;
import com.ddwl.schema.bo.goods.ItemSearchBo;
import com.ddwl.schema.bo.order.QuerySeckillBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.goods.CollectVo;
import com.ddwl.schema.vo.goods.ItemVo;
import com.ddwl.schema.vo.order.SeckillItemVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Fu Zhaohui
 * @version 1.0.0
 * @date 2019/7/15 12:55
 */
@Service
public class CollectService {

    @Autowired
    private CollectMapper collectMapper;

    @Resource
    private ItemDao itemDao;
    @Autowired
    private OrderApi orderApi;

    public List<ItemVo> page(PageBo<ItemSearchBo> pageBo) {
        PageHelper.startPage(pageBo.getPage(), pageBo.getPageSize()).count(false);
        List<Collect> entities = collectMapper.findAll(pageBo.getParam());
        if (entities.isEmpty()) {
            return new ArrayList<>(0);
        }
        List<String> itemIds = entities.stream().map(Collect::getItemId).distinct().collect(Collectors.toList());
        return itemDao.findListByIds(itemIds);
//        List<Item> items = itemMapper.findByPks(itemIds);
//        Map<String, Item> itemMap = items.stream().collect(Collectors.toMap(Item::getId, Function.identity(), (k1, k2) -> k1));
//        return entities.stream().map(entity -> {
//            ItemVo vo = new ItemVo();
//            BeanUtils.copyProperties(itemMap.get(entity.getItemId()), vo);
//            return vo;
//        }).collect(Collectors.toList());
    }

    @Transactional(rollbackFor = Throwable.class)
    public void collect(CollectBo bo) {
        Collect entity = new Collect();
        BeanUtils.copyProperties(bo, entity);
        if (0 == bo.getCollect()) {
            collectMapper.deleteItem(entity);
        } else {
            collectMapper.deleteItem(entity);
            entity.setId(UUIDGenerator.getShortUUID());
            collectMapper.insert(entity);
        }
    }

    public PageVo<CollectVo> getCollectPage(PageBo<ItemSearchBo> pageBo, String userId) {
        PageHelper.startPage(pageBo.getPage(),pageBo.getPageSize(), MySqlUtil.orderBy(pageBo.getSort(),pageBo.isAsc()));
        pageBo.getParam().setUserId(userId);
        PageInfo<CollectVo> info = new PageInfo(collectMapper.queryCollectList(pageBo.getParam()));
        List<String> itemIdList = info.getList().stream().map(CollectVo::getItemId).collect(Collectors.toList());
        if(!CollectionUtils.isEmpty(itemIdList)){
            QuerySeckillBo bo =new QuerySeckillBo();
            bo.setItemIds(itemIdList);
            bo.setTime(Instant.now());
            List<SeckillItemVo> seckillItemVos = orderApi.querySeckillItemList(bo).getResult();
            if (!CollectionUtils.isEmpty(seckillItemVos)){
                for (CollectVo collectVo:info.getList()) {
                    for (SeckillItemVo seckillItemVo:seckillItemVos) {
                        if (seckillItemVo.getItemId().equals(collectVo.getItemId())){
                            collectVo.setSeckillPrice(seckillItemVo.getSeckillPrice());
                        }
                    }
                }
            }
        }
        return new PageVo<>(info.getTotal(),pageBo.getPage(),pageBo.getPageSize(),info.getList());
    }
}
